package controller

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"yak.admin/src/entity"
	"yak.admin/src/middleware"
	"yak.admin/src/module/backend/hnyj/form"
	"yak.admin/src/module/backend/hnyj/repository"
	"yak.admin/src/util"
)

func DiscussCategory(c *gin.Context) {
	admin := c.MustGet(util.AdminKey).(entity.Admin)
	discussSearch := repository.DiscussRepository{Province: admin.Province, City: admin.City, County: admin.County, IsSuper: admin.IsSuper()}
	data, count := discussSearch.FindAllCategory(c)
	middleware.Datalist(c, data, count)
}

func Discusses(c *gin.Context) {
	admin := c.MustGet(util.AdminKey).(entity.Admin)
	discussSearch := repository.DiscussRepository{
		Province: admin.Province, City: admin.City, County: admin.County, IsSuper: admin.IsSuper(),
	}
	data, count := discussSearch.FindAll(c)
	middleware.Datalist(c, data, count)
}

func DiscussView(c *gin.Context)  {
	var page middleware.Page
	var search repository.DiscussRepository
	id, _ := strconv.Atoi(c.Query("id"))
	page.Tpl = "hnyj/discuss/detail-view.tpl"
	page.Data = map[string]interface{}{
		"discuss": search.FindDiscuss(uint(id)),
	}
	c.Set("Page", page)
}

func Wanjie(c *gin.Context)  {
	f := form.DiscussWanjieForm{}
	if err := util.DefaultGetValidParams(c, &f); err != nil {
		middleware.ResultFail(c, err.Error(), nil)
		return
	}
	f.Wanjie()
	middleware.ResultSuccess(c, "提交成功", nil)
}
